/**
 * 
 */
package dms.presentation.action.teacher;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import dms.model.School;
import dms.model.form.DormitoryChange;
import dms.model.role.StudentAffairTeacher;
import dms.model.role.wrapper.StudentAffairTeacherWrapper;
import dms.model.util.PageIterator;
import dms.service.DormitoryChangeQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;

/**
 * @author 李健
 *
 */
public class PropertyWorkAction extends MappingDispatchAction {
	private static Log log = LogFactory.getLog(PropertyWorkAction.class);
	
	public ActionForward DormitoryChangeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper)ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
		StudentAffairTeacher studentAffairTeacher = wrapper.getStudentAffairTeacher();
		DormitoryChangeQueryService dormitoryChangeQueryService = (DormitoryChangeQueryService ) ContextUtil.getContext().getBean("dormitoryChangeQueryService ");
		School school = wrapper.getStudentAffairTeacher().getSchool();
		PageIterator<DormitoryChange> dormitoryChange = null;
		Integer start = null;
		Integer count = null;
		int[] params = null;
		try {
			params = PageUtil.parsePageParameter(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		start = params[0];
		count = params[1];
		
		dormitoryChange = dormitoryChangeQueryService.findBySchool(start, count, school, null, null);
		request.setAttribute("dormitoryChange", dormitoryChange);
		request.setAttribute("studentAffairTeacher", studentAffairTeacher );
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward processRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long dormitoryChangeId = null;
		StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper)ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
		StringBuilder responseXML = new StringBuilder();
		try {
			dormitoryChangeId = Long.valueOf(request.getParameter("id"));
			DormitoryChange dormitoryChange =wrapper.getDormitoryChangeRepository().findById(dormitoryChangeId);
			wrapper.processDormitoryChange(dormitoryChange);
			responseXML.append("<response>");
			responseXML.append("<repair>");
			responseXML.append("<process>");
			responseXML.append("true");
			responseXML.append("</process>");
			responseXML.append("<message>");
			responseXML.append("寝室更换" + dormitoryChangeId + "已成功受理");
			responseXML.append("</message>");
			responseXML.append("</repair>");
			responseXML.append("</response>");
		} catch (Exception e) {
			log.error(e.getMessage());
			responseXML.append("<response>");
			responseXML.append("<repair>");
			responseXML.append("<process>");
			responseXML.append("false");

			responseXML.append("</process>");
			responseXML.append("<error-message>");
			responseXML.append("错误：寝室更换" + dormitoryChangeId + "或其它系统原因");
			responseXML.append("</error-message>");
			responseXML.append("</repair>");
			responseXML.append("</response>");
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.print(responseXML.toString());
		return  null;
	}
	
	public ActionForward applyRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long dormitoryChangeId = null;
		StudentAffairTeacherWrapper wrapper = (StudentAffairTeacherWrapper)ContextUtil.getContext().getBean("studentAffairTeacherWrapper");
		StringBuilder responseXML = new StringBuilder();
		try {
			dormitoryChangeId = Long.valueOf(request.getParameter("id"));
			DormitoryChange dormitoryChange =wrapper.getDormitoryChangeRepository().findById(dormitoryChangeId);
			wrapper.applyDormitoryChange(dormitoryChange);
			responseXML.append("<response>");
			responseXML.append("<repair>");
			responseXML.append("<process>");
			responseXML.append("true");
			responseXML.append("</process>");
			responseXML.append("<message>");
			responseXML.append("寝室更换" + dormitoryChangeId + "已成功受理");
			responseXML.append("</message>");
			responseXML.append("</repair>");
			responseXML.append("</response>");
		} catch (Exception e) {
			log.error(e.getMessage());
			responseXML.append("<response>");
			responseXML.append("<repair>");
			responseXML.append("<process>");
			responseXML.append("false");

			responseXML.append("</process>");
			responseXML.append("<error-message>");
			responseXML.append("错误：寝室更换" + dormitoryChangeId + "或其它系统原因");
			responseXML.append("</error-message>");
			responseXML.append("</repair>");
			responseXML.append("</response>");
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.print(responseXML.toString());
		return null;
	}
	
	
}
